package com.lrkj.property.service.impl;

import java.util.List;

import com.lrkj.common.constant.OrgConstants;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.property.domain.Community;
import com.lrkj.property.domain.Floor;
import com.lrkj.property.domain.Unit;
import com.lrkj.property.service.IFloorService;
import com.lrkj.property.service.IUnitService;
import com.lrkj.system.service.IOrgTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lrkj.property.mapper.ManageAreaMapper;
import com.lrkj.property.domain.ManageArea;
import com.lrkj.property.service.IManageAreaService;

/**
 * 管理区Service业务层处理
 *
 * @author yajun_ren
 * @date 2021-07-28
 */
@Service
public class ManageAreaServiceImpl implements IManageAreaService
{
    @Autowired
    private ManageAreaMapper manageAreaMapper;

    @Autowired
    private IUnitService unitService;

    @Autowired
    private IFloorService floorService;

    @Autowired
    private IOrgTreeService orgTreeService;

    /**
     * 查询管理区
     *
     * @param areaId 管理区ID
     * @return 管理区
     */
    @Override
    public ManageArea selectManageAreaById(String areaId)
    {
        return manageAreaMapper.selectManageAreaById(areaId);
    }

    /**
     * 查询管理区列表
     *
     * @param manageArea 管理区
     * @return 管理区
     */
    @Override
    public List<ManageArea> selectManageAreaList(ManageArea manageArea)
    {
        return manageAreaMapper.selectManageAreaList(manageArea);
    }

    /**
     * 新增管理区
     *
     * @param manageArea 管理区
     * @return 结果
     */
    @Override
    public int insertManageArea(ManageArea manageArea)
    {
        /*String s[]=manageArea.getFloorId().split(",");
        String ids=null;
        for (int i = 0; i < s.length; i++) {
            ids=s[i];
        }
        Floor floor =floorService.selectFloorById(ids);
        manageArea.setDeptId(floor.getDeptId());*/
        manageArea.setAreaId(IdUtils.simpleUUID());
        manageArea.setCreateBy(SecurityUtils.getUsername());
        manageArea.setCreateTime(DateUtils.getNowDate());
        this.setUnitInfo(manageArea);
        return manageAreaMapper.insertManageArea(manageArea);
    }

    /**
     * 修改管理区
     *
     * @param manageArea 管理区
     * @return 结果
     */
    @Override
    public int updateManageArea(ManageArea manageArea)
    {
        manageArea.setUpdateBy(SecurityUtils.getUsername());
        manageArea.setUpdateTime(DateUtils.getNowDate());
        this.setUnitInfo(manageArea);
        return manageAreaMapper.updateManageArea(manageArea);
    }

    /**
     * 批量删除管理区
     *
     * @param areaIds 需要删除的管理区ID
     * @return 结果
     */
    @Override
    public int deleteManageAreaByIds(String[] areaIds)
    {
        orgTreeService.deleteOrgTreeByIds(areaIds);
        return manageAreaMapper.deleteManageAreaByIds(areaIds);
    }

    /**
     * 删除管理区信息
     *
     * @param areaId 管理区ID
     * @return 结果
     */
    @Override
    public int deleteManageAreaById(String areaId)
    {
        orgTreeService.deleteOrgTreeById(areaId);
        return manageAreaMapper.deleteManageAreaById(areaId);
    }


    private void setUnitInfo(ManageArea bean) {
        String s[]=bean.getFloorId().split(",");
        String ids=null;
        for (int i = 0; i < s.length; i++) {
            ids=s[i];
        }
        Floor parentBean =floorService.selectFloorById(ids);
        bean.setAncestors(parentBean.getAncestors() + "," + parentBean.getId());
        bean.setDeptId(parentBean.getDeptId());
        orgTreeService.insertOrgTree(bean.getAreaId(), parentBean.getCommunityName(), parentBean.getId(), OrgConstants.area, parentBean.getAncestors());
    }
}
